<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use App\Models\Admin\Stats\User\Day as StatsDay;
use App\Models\Admin\Stats\User\Week as StatsWeek;
use App\Models\Admin\Stats\User\Month as StatsMonth;
use App\Models\Admin\StatsTask\User\Day as TaskDay;
use App\Models\Admin\StatsTask\User\Week as TaskWeek;
use App\Models\Admin\StatsTask\User\Month as TaskMonth;
use App\Core\Traits\Numeral;

class CreateStatsUserTable extends Migration
{
    use Numeral;

    /**
     * 统计任务表字段
     */
    private function makeStatsTask($tableName)
    {
        return function (Blueprint $table) use ($tableName) {
            $table->id();
            $table->unsignedInteger('timed');
            $table->string('plat_code', 64)->comment('平台code');

            $table->tinyInteger('attempts')->comment('任务尝试次数')->default(0);
            $table->tinyInteger('status')->comment('状态：1初始化，2推送到队列，3开始处理，4成功，5失败')->default(1)->index();

            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');

            // 唯一索引
            $table->unique(['timed', 'plat_code'], "{$tableName}_unique");
        };
    }

    /**
     * 统计数据表字段
     */
    private function makeStats($tableName)
    {
        return function (Blueprint $table) use ($tableName) {
            $table->id();
            $table->unsignedInteger('timed');
            $table->string('plat_code', 64)->comment('平台code')->index();

            $table->integer('u_game')->comment('游戏人数')->nullable();
            $table->integer('u_sign')->comment('登录人数')->nullable();
            $table->integer('nu_game')->comment('新增游戏人数')->nullable();
            $table->integer('nu_sign')->comment('注册人数')->nullable();

            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');

            // 唯一索引
            $table->unique(['timed', 'plat_code'], "{$tableName}_unique");
        };
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /**
         * 统计任务表【天】
         */
        $tableName = TaskDay::tableName();
        // 创建表结构
        TaskDay::schemaConnection()->create($tableName, $this->makeStatsTask($tableName));
        // 表名注释
        TaskDay::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--用户--天'");
        /**
         * 统计数据表【天】
         */
        $tableName = StatsDay::tableName();
        // 创建表结构
        StatsDay::schemaConnection()->create($tableName, $this->makeStats($tableName));
        // 表名注释
        StatsDay::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--用户--天'");

        /**
         * 统计任务表【周】
         */
        $tableName = TaskWeek::tableName();
        // 创建表结构
        TaskWeek::schemaConnection()->create($tableName, $this->makeStatsTask($tableName));
        // 表名注释
        TaskWeek::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--用户--周'");
        /**
         * 统计数据表【周】
         */
        $tableName = StatsWeek::tableName();
        // 创建表结构
        StatsWeek::schemaConnection()->create($tableName, $this->makeStats($tableName));
        // 表名注释
        StatsWeek::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--用户--周'");

        /**
         * 统计任务表【月】
         */
        $tableName = TaskMonth::tableName();
        // 创建表结构
        TaskMonth::schemaConnection()->create($tableName, $this->makeStatsTask($tableName));
        // 表名注释
        TaskMonth::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--用户--月'");
        /**
         * 统计数据表【月】
         */
        $tableName = StatsMonth::tableName();
        // 创建表结构
        StatsMonth::schemaConnection()->create($tableName, $this->makeStats($tableName));
        // 表名注释
        StatsMonth::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--用户--月'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // 统计任务表【天】
        TaskDay::schemaConnection()->dropIfExists(TaskDay::tableName());
        // 统计数据表【天】
        StatsDay::schemaConnection()->dropIfExists(StatsDay::tableName());

        // 统计任务表【周】
        TaskWeek::schemaConnection()->dropIfExists(TaskWeek::tableName());
        // 统计数据表【周】
        StatsWeek::schemaConnection()->dropIfExists(StatsWeek::tableName());

        // 统计任务表【月】
        TaskMonth::schemaConnection()->dropIfExists(TaskMonth::tableName());
        // 统计数据表【月】
        StatsMonth::schemaConnection()->dropIfExists(StatsMonth::tableName());
    }
}
